package com.library.uiframework.widget.state;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.library.uiframework.R;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

/**
 * @author xinpeng.xiao
 * @version 1.0.0
 *          Created on 2016/1/21.
 */
public class StateViewHelper {

    private View.OnClickListener mOnRetryClickListener;

    private StateView.OnConfigStateViewListener mDefaultConfig = new StateView.OnConfigStateViewListener() {
        @Override
        public void onConfigStateView(View view, StateView.State state) {
            if (StateView.State.FAILED.equals(state)) {
                view.setOnClickListener(mOnRetryClickListener);
            }
        }
    };

    /**
     * @param view                 view
     * @param onRetryClickListener onRetryClickListener
     * @return StateView
     */
    public StateView inject(View view, View.OnClickListener onRetryClickListener) {
        mOnRetryClickListener = onRetryClickListener;

        StateView stateView = new StateView(view.getContext());
        stateView.setStateProperty(StateView.State.FAILED, R.layout.uiframework_loadingview_failed);
        stateView.setStateProperty(StateView.State.LOADING, R.layout.uiframework_loadingview_loading);
        stateView.setStateProperty(StateView.State.NO_DATA, R.layout.uiframework_loadingview_nodata);
        stateView.setStateProperty(StateView.State.SUCCESS, view);

        stateView.setOnConfigStateViewListener(mDefaultConfig);

        stateView.setLayoutParams(new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
        return stateView;
    }

    /**
     *
     * @param inflater      inflate
     * @param originView    原始的view
     * @param footerView    底部的view
     * @return  增加固定底部的view后
     */
    public static View wrapWithStickFooter(LayoutInflater inflater, View originView, View footerView) {
        return wrapWithLinearLayout(inflater, originView, footerView, 1);
    }

    /**
     *
     * @param inflater      inflate
     * @param originView    原始的view
     * @param headerView    头部的view
     * @return  增加固定头部的view后
     */
    public static View wrapWithStickHeader (LayoutInflater inflater, View originView, View headerView) {
        return wrapWithLinearLayout(inflater, originView, headerView, 0);
    }

    private static LinearLayout wrapWithLinearLayout(LayoutInflater inflater, View originView, View appendView, int appendViewIndex) {
        LinearLayout linearLayout = new LinearLayout(inflater.getContext());
        linearLayout.setLayoutParams(new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.addView(originView);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
        params.weight = 1;
        originView.setLayoutParams(params);

        linearLayout.addView(appendView, appendViewIndex);
        return linearLayout;
    }
}
